package com.itheima.web;


import com.itheima.service.PayService;
import com.itheima.support.Result;
import com.itheima.utils.PayUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
@RequestMapping("/pay")
public class PayController {

    @DubboReference
    private PayService payService;

    @Resource
    private RabbitTemplate rabbitTemplate;

    //获取支付链接
    @GetMapping("/getPayUrl/{id}")
    private Result getPayUrl(@PathVariable("id") long id) {

        //设置死信时间为15分钟
        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //设置消息超时时间为40s
                message.getMessageProperties().setExpiration("900000");
                return message;
            }
        };
//        System.out.println("消息发送时间:" + System.currentTimeMillis());
        rabbitTemplate.convertAndSend("pay_exchange", "pay", id, messagePostProcessor);

        return new Result(PayUtils.createOrder(String.valueOf(id), 1));// 写死1分钱...)
    }


}
